<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Redis;
use App\Models\CabinetLiveText;
use App\Models\CabinetPoint;

class FixLivetextCache extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'fix-livetext';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command. 
     *
     * @return mixed
     */
    public function handle()
    {   
        echo "start\n";
        foreach (CabinetLiveText::cursor() as $oneText) {
            if ($oneText['point_alias']) {
                $alias = $oneText['point_alias'];
                $cacheKey = CabinetLiveText::PREFIX.$alias;
            } else {
                $point = CabinetPoint::find($oneText['point_id']);
                if ($point) {
                    $alias = $point['alias'];
                    $oneText->point_alias = $alias;
                    $oneText->save();
                    $cacheKey = CabinetLiveText::PREFIX.$alias;
                }
            }

            if (isset($cacheKey)) {
                $textData = [
                    'name' => $oneText['name'],
                    'cond' => $oneText['condition'],
                    'val' => $oneText['value'],
                ];
                CabinetLiveText::setTextCache($alias, $textData);
            }
            // error_log("oneText = ".print_r($oneText,1)."\n",3,'/phplogs/vv.log');
        }
        echo "done\n";
    }
}
